package org.wmine.algo;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import org.wmine.core.IProjectManager;
import org.wmine.core.ProjectManager;

public class BasAlgoWriter {
	Document xmlDocument;
	String file;
	public BasAlgoWriter(String filename,String rut) {
		xmlDocument = null;
		file=filename;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		try {
			builder = factory.newDocumentBuilder();
		} catch (ParserConfigurationException e3) {
			e3.printStackTrace();
		}
		DOMImplementation impl = builder.getDOMImplementation();
		Element e1 = null,e2 = null,e3 = null;
		Node n = null;
		xmlDocument = impl.createDocument(null, rut, null);
	}
	
	/*to add element directly to root*/
	public void addElement(String elementName,String valueName ) {
		Element root = xmlDocument.getDocumentElement();
		Element e1 = xmlDocument.createElementNS(null, elementName);
		Text n = xmlDocument.createTextNode(valueName);
		e1.appendChild(n);
		root.appendChild(e1);
	}
	
	/*to add elements to element1 and then element1 to root*/
	public void addElement(String elementName1,String []elementName2,String []valueName ) {
		Element root = xmlDocument.getDocumentElement();
		Element e1 = xmlDocument.createElementNS(null, elementName1);
		Element e2 ;
		Text n;
		for(int i=0;i<elementName2.length;i++){
			e2= xmlDocument.createElementNS(null, elementName2[i]);
			n = xmlDocument.createTextNode(valueName[i]);
	
			e2.appendChild(n);
			e1.appendChild(e2);
		}
		root.appendChild(e1);
		}
		
		
		public void addElement(String elementName1,String [][]elementName3,String [][]valueName ) {
		Element root = xmlDocument.getDocumentElement();
	
		Element e2=null,e3 =null,e1=null;
		Text n;
	
		for(int i=0;i<elementName3.length;i++){
			 e1 = xmlDocument.createElementNS(null, elementName1);
		
			for(int j=0;j<elementName3[i].length;j++){
				
				e3= xmlDocument.createElementNS(null, elementName3[i][j]);
			//	e3=xmlDocument.createElement(elementName3[i][j]);
				n = xmlDocument.createTextNode(valueName[i][j]);
				e3.appendChild(n);
				e1.appendChild(e3);
				
			}
				
			root.appendChild(e1);
		}
	
		
		}
			
	public void addElement(String elementName1,String elementName2,String [][]elementName3,String [][]valueName ) {
		Element root = xmlDocument.getDocumentElement();
		Element e1 = xmlDocument.createElementNS(null, elementName1);
		Element e2=null,e3 =null;
		Text n;
	
		for(int i=0;i<elementName3.length;i++){
		
			e2=xmlDocument.createElementNS(null, elementName2);
			for(int j=0;j<elementName3[i].length;j++){
				e3= xmlDocument.createElementNS(null, elementName3[i][j]);
			//	e3=xmlDocument.createElement(elementName3[i][j]);
				n = xmlDocument.createTextNode(valueName[i][j]);
				e3.appendChild(n);
				e2.appendChild(e3);
				
			}
				e1.appendChild(e2);
			
		}
	
		root.appendChild(e1);
		}
	
	
	public String getXMLString() {
		
		StringWriter stringWriter = new StringWriter();
		
		DOMSource domSource = new DOMSource(xmlDocument);
		StreamResult streamResult = new StreamResult(stringWriter);
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer serializer = null;
		try {
			serializer = tf.newTransformer();
			serializer.setOutputProperty(OutputKeys.INDENT,"yes");
			serializer.transform(domSource, streamResult);
			
		} 
		catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} 
		catch (TransformerException e) {
			e.printStackTrace();
		}
		return stringWriter.toString();//It prints all XML in String format 
	}

	boolean save()
	{	IProjectManager pm=ProjectManager.getUniqueInstance();
		try{
			//pm.createProject("sh","j:\\annu");
			pm.openProject("j:\\annu\\sh");
			pm.writeFile(file,getXMLString());
			//pm.writeFile("stat-res.xml",getXMLString());
		}
		catch(Exception e){
			return false;}
			
		return true;
		}
		
	public static void main(String[] args) {
		//BasAlgoWriter b = new BasAlgoWriter();
		//b.addElement(null,null);
		//xcSystem.out.println(b.getXMLString());
	
	}
	
	
	
}
